package org.fujene.memman.rank;

import java.util.Comparator;
import java.util.TreeSet;

import org.fujene.constants.Constants;
import org.fujene.structure.ExtendResult;
import org.fujene.structure.Queue;

public class ReplicateReranker{
	// Re-rank by score
	public static void rerank(ExtendResult result, int type){
		long time = System.nanoTime();
		TreeSet<Queue> reranked;
		if(type == Constants.RERANK_TFIDF){
			reranked = new TreeSet<Queue>(new Comparator<Queue>(){
				@Override
				public int compare(Queue q1, Queue q2){
					return (q1.score < q2.score ? 1 : (q1.score > q2.score ? -1 : 0));
				}
			});
		}else{
			reranked = new TreeSet<Queue>(new Comparator<Queue>(){
				@Override
				public int compare(Queue q1, Queue q2){
					return (q1.ID < q2.ID ? 1 : (q1.ID > q2.ID ? -1 : 0));
				}
			});
		}
		for(int i = 0; i < result.getQueues().length; i++){
			Queue queue = result.getQueue(i);
			if(queue != null) reranked.add(queue);
		}

		result.setResult(reranked.toArray(new Queue[0]), result.getTime() + (System.nanoTime() - time)
				/ 1000000000.0);
	}
}
